#pragma once
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <cstring>
#include <pthread.h>
#include <memory>

#include "Log.hpp"
#include "InetAddr.hpp"

using namespace log_ns;

const int gblcklog = 8;

enum
{
    SOCKET_ERROR = 1,
    BIND_ERROR,
    LISTEN_ERR
};

namespace socket_ns
{
    using SockPtr = std::shared_ptr<socket>;
    class socket
    {
    public:
        virtual void CreateSocketOrDie() = 0;
        virtual void CreateBindOrDie(uint16_t port) = 0;
        virtual void CreateListenOrDie(int backlog) = 0;
        virtual SockPtr Accepter(InetAddr *cliaddr) = 0;
        virtual void Connector() = 0;

    public:
        void BuildListenSocket()
        {
            void CreateSocketOrDie();
            void CreateBindOrDie();
            void CreateListenOrDie();
        }
    };

    class TcpSocket : public socket
    {
    public:
        TcpSocket(int sockfd) : _sockfd(sockfd)
        {
        }
        void CreateSocketOrDie() override
        {
            // 1.创建socket
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(FATAL, "socket create error\n");
                exit(SOCKET_ERROR);
            }
            LOG(INFO, "socket create success, sockfd: %d\n", _sockfd);
        }

        void CreateBindOrDie(uint16_t port) override
        {
            struct sockaddr_in local;
            memset(&local, 0, sizeof(local));

            local.sin_port = htons(port);
            local.sin_family = AF_INET;
            local.sin_addr.s_addr = INADDR_ANY;

            // 2.bind socket 和 addr
            if (::bind(_sockfd, (struct sockaddr *)&local, sizeof(local)) < 0)
            {
                LOG(FATAL, "bind error\n");
                exit(BIND_ERROR);
            }

            LOG(INFO, "bind success\n");
        }

        void CreateListenOrDie(int backlog = gblcklog) override
        {
            // 3. tcp 是面向连接的，tcp需要未来不断地能够做到获取连接
            if (::listen(_sockfd, gblcklog) < 0)
            {
                LOG(FATAL, "listen error\n");
                exit(LISTEN_ERR);
            }
            LOG(INFO, "listen success\n");
        }

        SockPtr Accepter(InetAddr *cliaddr) override
        {
            struct sockaddr_in client;
            socklen_t len = sizeof(client);
            // 4. 获取新连接
            int sockfd = ::accept(_sockfd, (struct sockaddr *)&client, &len);

            if (sockfd < 0)
            {
                LOG(WARNING, "accept error\n");
                return nullptr;
            }
            *cliaddr = InetAddr(client);
            LOG(INFO, "get a new link, client info : %s, sockfd : %d\n",
                cliaddr->AddrStr().c_str(), sockfd);

            return std::make_shared<socket>(sockfd);
        }

        void Connector() override
        {
            // struct sockaddr_in server;
            // server.sin_family = AF_INET;
            // server.sin_port = htons(serverport);
            // ::inet_pton(AF_INET, serverip.c_str(), &server.sin_addr);

            // int n = ::connect(sockfd, (struct sockaddr *)&server, sizeof(server));
            // if (n < 0)
            // {
            //     std::cerr << "connect socket error" << std::endl;
            //     exit(2);
            // }
        }

    private:
        int _sockfd;
    };

};
